package servlet;

import dao.CourseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/update_course")
public class update_course extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        CourseDao courseDao = new CourseDao();

        String[] name = request.getParameterValues("name");
        String[] teacher = request.getParameterValues("teacher");
        String[] college = request.getParameterValues("college");
        String[] stu_ids = request.getParameterValues("stu_id");
        String[] id = request.getParameterValues("c_id");

        try {
            for (int i=0; i<id.length; i++) {
                courseDao.updateCourseInfo(name[i], teacher[i], college[i],stu_ids[i],id[i]);
            }
            response.sendRedirect("one_page_course");
        }
        catch (Exception e){
            out.print(e);
        }
    }
}
